<?php

namespace App\Http\Services\CoolNumberShoppingMall;

use App\Http\Model\CoolNumberShoppingMall\GoodsTag;
use App\Http\Services\CommonServices;
use Illuminate\Http\Request;

/**
 * 靓号库，商品标签管理
 *
 * Created on 2021/12/7 11:29
 * @Author <yuxiang.zhu@asiainnovations.net>
 */
class CoolNumberTagServices extends CommonServices
{
    /**
     * @var GoodsTag
     */
    private $modelGoodsTag;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->modelGoodsTag = new GoodsTag();
    }

    /**
     * 添加/编辑商品标签
     *
     * Created at 2021/12/6 18:17
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param array $params
     */
    public function addAndUpdate(array $params)
    {
        $id = intval($params['id'] ?? 0);
        $tagName = $params['tag_name'] ?? '';
        $tagImage = $params['tag_image'] ?? '';
        if (!$tagName || !$tagImage) {
            return;
        }

        $columns = ['tag_name' => $tagName, 'tag_image' => $tagImage, 'updated_at' => time()];
        if ($id) {
            $this->modelGoodsTag->singleUpdate(['id' => $id], $columns);
        } else {
            $columns['created_at'] = time();
            $this->modelGoodsTag->insertSingle($columns);
        }
    }

    /**
     * 获取所有标签
     *
     * Created at 2021/12/10 10:49
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @return array
     */
    public function getAllTags(): array
    {
        return $this->modelGoodsTag->getSingle([], ['id', 'tag_name', 'tag_image']);
    }
}